import GlobalCache from "../GlobalCache";
import { Http2Stream } from "./Http2Stream";
import buffer from '@ohos.buffer';

export type Buffer = buffer.Buffer;

export class ClientHttp2Stream extends Http2Stream {
  _handle;
  _fd;

  constructor(session, handle, fd, options) {
    super(session, options);
    this._handle = handle;
    this._fd = fd;
  }

  on(event: string, callback: (...args: any[]) => void) {
    GlobalCache.onCallBackMap.set(this._handle + event, callback);
  }

  write(buf: buffer.Buffer) {
    let arrayBuffer: ArrayBuffer = new ArrayBuffer(buf.length);
    let arr: Uint8Array = new Uint8Array(arrayBuffer);
    for (let i: number = 0; i < buf.length; i++) {
      arr[i] = buf[i];
    }
    this._handle.write(this._fd + "", arrayBuffer);
  }

  resume() {
    // this._handle.resume(this._fd + "");
  }

  pause() {
    // this._handle.pause(this._fd + "");
  }

  destroyed() {
    // this._handle.destroyed(this._fd + "");
  }

  end() {
      //TODO 不能在这里调用 会有时机问题 请求未结束
      // this._handle.end(this._fd + "");
  }

  close(code: number): void {
    // console.log(`Closing with code: ${code}`);
    // this._handle.close(this._fd + "", code + "");
  }

  ping(buffer:string){
     // this._handle.ping(this._fd + "", buffer);
  }

  getWindowSize():string{
    return "";
  }

}